<?php

/**
 * 表单HTML
 */
namespace app\site\model;

class SiteFormFieldHtmlModel {

    public function text($label, $must, $tip, $data, $config) {
        $must = $must ? 'required' : '';
        $config = explode(',', $config, 2);
        $min = 0;
        $max = 0;
        if ($config[0]) {
            $min = intval($config[0]);
        }
        if ($config[1]) {
            $max = intval($config[1]);
        }
        $min = $min ? ' minlength="' . $min . '" ' : '';
        $max = $max ? ' maxlength="' . $max . '" ' : '';
        return '<input type="text" name="' . $label . '" value="' . $data . '" placeholder="' . $tip . '" ' . $must . $min . $max . '>';
    }

    public function number($label, $must, $tip, $data, $config) {
        $must = $must ? 'required' : '';
        $config = explode(',', $config, 2);
        $min = 0;
        $max = 0;
        if ($config[0]) {
            $min = intval($config[0]);
        }
        if ($config[1]) {
            $max = intval($config[1]);
        }
        $min = $min ? ' min="' . $min . '" ' : '';
        $max = $max ? ' max="' . $max . '" ' : '';
        return '<input type="number" name="' . $label . '" value="' . $data . '" placeholder="' . $tip . '" ' . $must . $min . $max . '>';
    }

    public function phone($label, $must, $tip, $data, $config) {
        $must = $must ? 'required' : '';
        $config = explode(',', $config, 2);
        if (empty($config)) {
            $reg = '/(^1[3|4|5|7|8][0-9]{9}$)/';
        } else {
            $configReg = implode('|', $config);
            $reg = '/(^[' . $configReg . '][0-9]{8}$)/';
        }
        return '<input type="text" name="' . $label . '" value="' . $data . '" placeholder="' . $tip . '" ' . $must . ' pattern="' . $reg . '">';
    }

    public function tel($label, $must, $tip, $data, $config) {
        $must = $must ? 'required' : '';
        $config = explode(',', $config, 2);
        if (empty($config)) {
            $reg = '/^([0-9]{3,4}-)?[0-9]{7,8}$/';
        } else {
            $configReg = implode('|', $config);
            $reg = '/^([' . $configReg . ']-)?[0-9]{7,8}$/';
        }
        return '<input type="text" name="' . $label . '" value="' . $data . '" placeholder="' . $tip . '" ' . $must . ' pattern="' . $reg . '">';
    }

    public function email($label, $must, $tip, $data, $config) {
        return $data;
    }

    public function textarea($label, $must, $tip, $data, $config) {
        return $data;
    }

    public function editor($label, $must, $tip, $data, $config) {
        return $data;
    }

    public function date($label, $must, $tip, $data, $config) {
        return date('Y-m-d', $data);
    }

    public function time($label, $must, $tip, $data, $config) {
        $time = explode(':', $data);
        return $time[0] . '点' . $time[1] . '分' . $time[2] . '秒';
    }

    public function datetime($label, $must, $tip, $data, $config) {
        return date('Y-m-d H:i:s', $data);
    }

    public function select($label, $must, $tip, $data, $config) {
        return $this->listSlt($label, $must, $tip, $data, $config);

    }

    public function radio($label, $must, $tip, $data, $config) {
        return $this->listSlt($label, $must, $tip, $data, $config);
    }

    public function checkbox($label, $must, $tip, $data, $config) {
        return $this->listSlt($label, $must, $tip, $data, $config);
    }

    public function image($label, $must, $tip, $data, $config) {
        return "<img src='{$data}' width='120' height='80'>";
    }

    public function images($label, $must, $tip, $data, $config) {
        $images = unserialize($data);
        return "<img src='{$images[0]}' width='120' height='80'>";
    }

    public function file($label, $must, $tip, $data, $config) {
        return $data;
    }

    public function files($label, $must, $tip, $data, $config) {
        $files = unserialize($data);
        $html = '';
        foreach ($files as $key => $vo) {
            if ($key) {
                $html .= '<br>';
            }
            $html .= $vo;
        }
        return $html;
    }

    public function price($label, $must, $tip, $data, $config) {
        return number_format($data, 2);
    }

    public function color($label, $must, $tip, $data, $config) {
        return $data;
    }

    public function area($label, $must, $tip, $data, $config) {
        $area = explode(',', $data);
        return $area[0] . ' - ' . $area[1] . ' - ' . $area[2];
    }

    public function baidumap($label, $must, $tip, $data, $config) {
        return $data;
    }

    protected function listSlt($label, $must, $tip, $data, $config = []) {
        $list = explode("\n", $config);
        $list = array_filter($list);
        foreach ($list as $vo) {
            $info = explode(',', $vo);
            if ($info[0] == $data) {
                return $info[1];
            }
        }
    }
}